package com.example.studentportrait.config;

import com.example.studentportrait.filter.JwtAuthenticationTokenFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

@Configuration
@EnableWebSecurity
@EnableMethodSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    JwtAuthenticationTokenFilter jwtAuthenticationTokenFilter;

    @Autowired
    AuthenticationEntryPoint authenticationEntryPoint;

    @Autowired
    AccessDeniedHandler accessDeniedHandler;
    //密码加密
    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                //关闭csrf
                .csrf().disable()
                //不通过Session获取SecurityContext
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .authorizeRequests()
                // 对于登录接口 允许匿名访问
                .antMatchers("/user/login","/user/register").anonymous()
                .antMatchers("/admin/updateSchool").anonymous()
                .antMatchers("/admin/addSchool").anonymous()
                .antMatchers("/admin/deleteSchool/{id}").anonymous()
                .antMatchers("/admin/schoolList").anonymous()
                .antMatchers("/admin/deleteSchoolList").anonymous()//公告批量删除
                .antMatchers("/admin/insertSchoolListByExcel").anonymous()//项目列表
                .antMatchers("/user/teacher/reviewItemById").anonymous()//审核项目
                .antMatchers("/user/teacher/competitionList").anonymous()//比赛列表
                .antMatchers("/user/teacher/reviewCompetitionById").anonymous()//审核比赛
                .antMatchers("/user/teacher/companyInternshipList").anonymous()//实习列表
                .antMatchers("/user/teacher/reviewCompanyInternshipById").anonymous()//审核实习
                .antMatchers("/user/teacher/activityList").anonymous()//活动列表
                .antMatchers("/user/teacher/reviewActivityById").anonymous()//审核活动
                .antMatchers("/user/teacher/specialStuReviewList").anonymous()//特殊情况学生审核列表
                .antMatchers("/user/teacher/reviewSpecialStuById").anonymous()//审核特殊情况学生
//                // 个人信息接口和登出接口需登录后才能访问
//                .antMatchers("/logout").authenticated()
                .antMatchers("/user/userInfo").authenticated()
                .antMatchers("/code/verify").permitAll()
//                .antMatchers("/upload").authenticated()
                // 除上面外的所有请求全部需要认证即可访问
                .anyRequest().authenticated();

//        配置异常处理器
        http.exceptionHandling()
                .authenticationEntryPoint(authenticationEntryPoint)
                .accessDeniedHandler(accessDeniedHandler);

//        关闭默认注销接口的请求地址
        http.logout().disable();
//        添加jwtAuthenticationTokenFilter到springsecurity的过滤器链中
        http.addFilterBefore(jwtAuthenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);
        //允许跨域
        http.cors();
    }
    @Override
    @Bean
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }
}
